import { _decorator, Component, Label, Node, Vec3 } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { GAME_POP, GAME_SCENE, SOUND_TYPE } from '../../Data/GlobalData';
import { Tools } from '../../Tool/Tools';
import { PopStart } from '../../Pop/PopStart';
import { PopSet } from '../../Pop/PopSet/PopSet';
import { PopCollect } from '../../Pop/PopCollect';
import { PopLevel } from '../../Pop/PopLevel';
const { ccclass, property } = _decorator;

@ccclass('LoginScene')
/**
 * 创建一个通用角色
 * 写一个通用框架
 */
export class LoginScene extends BaseComponent {

    //金币文字
    @property(Label)
    private CoinLabel: Label = null;

    //体力文字
    @property(Label)
    private PowerLabel: Label = null;

    //最高分数
    @property(Label)
    private MaxScoreLabel: Label = null;

    //倒计时
    @property(Label)
    private TimeLabel: Label = null;

    //回复体力时间
    private ResetTime: number = 10 * 60;

    protected Init(): void {
    }
    protected OnShow(): void {
        app.gameData.Init();
        this.UpdatePower();
        this.UpdateGold();
        this.MaxScoreLabel.string = app.gameData.GameData.bestscore.toString();
        this.TimeLabel.node.active = false;

        if (app.gameData.GameData.power < 5) {

            if (app.gameData.GameData.gametime == 0) {
                app.gameData.GameData.gametime = Date.now();
                app.gameData.UpdateGameDateLocal();
            }

            let dis_time: number = Math.floor((Date.now() - app.gameData.GameData.gametime) / 1000);
            if (dis_time > 0) {
                app.gameData.AddPower(Math.floor(dis_time / this.ResetTime));
                this.UpdatePower();
            }

        }

    }

    protected update(dt: number): void {
        if (app.gameData.GameData.power < 5) {
            if (this.ResetTime - Math.floor((Date.now() - app.gameData.GameData.gametime) / 1000) <= 0) {
                app.gameData.GameData.gametime = Date.now();
                this.TimeLabel.node.active = false;
                app.gameData.AddPower(1);
                this.UpdatePower();
            } else {
                this.TimeLabel.node.active = true;
                this.TimeLabel.string = Tools.secondsFormat(this.ResetTime - Math.floor((Date.now() - app.gameData.GameData.gametime) / 1000), false)
                this.TimeLabel.node.setWorldPosition(this.PowerLabel.node.parent.getWorldPosition().add(new Vec3(0, -60)));
            }
        }
    }

    /**
     * 点击开始游戏
     */
    private StartClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        if (app.gameData.GameData.power == 0) {
            app.popManager.ShowPop(GAME_POP.PopStart)
            app.popManager.CurrentPop.getComponent(PopStart).Cb = () => {
                app.gameData.AddPower(1);
                this.UpdatePower();
            }
            return;
        }
        app.gameData.GameData.power--;
        app.gameData.UpdateGameDateLocal();
        app.uiManager.ShowUI(GAME_SCENE.GameScene);
    }

    /**
     * 更新体力
     */
    private UpdatePower() {
        this.PowerLabel.string = app.gameData.GameData.power.toString();

        if (app.gameData.GameData.power >= 5) {
            this.TimeLabel.node.active = false;
        }

    }

    /**
     * 更新金币
     */
    public UpdateGold() {
        this.CoinLabel.string = app.gameData.GameData.coin.toString();
    }

    /**
     * 点击设置
     */
    private SettingClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        app.popManager.ShowPop(GAME_POP.PopSet)
        app.popManager.CurrentPop.getComponent(PopSet).GoLoginBtn.active = false;
    }

    /**
     * 收藏点击
     */
    private CollectionClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        app.popManager.ShowPop(GAME_POP.PopCollect);
        app.popManager.CurrentPop.getComponent(PopCollect).UpdateCoin = this.UpdateGold.bind(this);
    }

    /**
     * 点击升级按钮
     */
    private LevelClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        app.popManager.ShowPop(GAME_POP.PopLevel);
        app.popManager.CurrentPop.getComponent(PopLevel).UpdateCoinCb = this.UpdateGold.bind(this);
    }

}


